package com.yuanmei.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yuanmei.common.Utils;

import java.util.Date;

public class QiMenWebViewActivity extends Activity{
    private final static String TAG = QiMenWebViewActivity.class.getName();
    private WebView mQiMenWV;
    private int mTimeToMarket;

    class QiMenPlatService {

        Activity mActivity;

        QiMenPlatService(Activity activity) {
            mActivity = activity;
        }

        @android.webkit.JavascriptInterface
        public void Log(String message) {
            Log.i("QiMenH5", message);
        }

        @android.webkit.JavascriptInterface
        public String getToMarketDate() {
            // dataStr = 2015-5-9 18:19
            Date marketDate = Utils.getDateFromIDate(mTimeToMarket);
            String dateString = Utils.sHyphenDateFormat.format(marketDate);
            dateString += " 09:30:00";
            return dateString;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String mName = getIntent().getStringExtra("Name");
        String mNetEaseCode = getIntent().getStringExtra("NetEaseCode");
        boolean mIsMarketIndex = getIntent().getBooleanExtra("IsMarketIndex", false);
        mTimeToMarket = getIntent().getIntExtra("TimeToMarket", 19700101);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_qimen_view);

        mQiMenWV = (WebView) findViewById(R.id.qimen_webview);

        WebSettings qimenSettings = mQiMenWV.getSettings();
        qimenSettings.setJavaScriptEnabled(true);
        qimenSettings.setSupportZoom(true);
        qimenSettings.setUseWideViewPort(true);
        qimenSettings.setBuiltInZoomControls(true);
        qimenSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);;
        qimenSettings.setAllowFileAccess(true);
        qimenSettings.setAllowFileAccessFromFileURLs(true);
        qimenSettings.setAllowContentAccess(true);
        qimenSettings.setDomStorageEnabled(true);
        qimenSettings.setAllowFileAccessFromFileURLs(true);

        mQiMenWV.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) {
                wv.loadUrl(url); //在当前的webview中跳转到新的url
                return false;
            }
        });
        mQiMenWV.addJavascriptInterface(new QiMenPlatService(this), "qmplat");
        mQiMenWV.loadUrl("file:///android_asset/www/index.html");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
